package com.xcm.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.google.common.collect.Lists;
import com.xcm.constant.DateFormatConstant;
import com.xcm.constant.OperationLogConstant;
import com.xcm.dao.DeviceLogMapper;
import com.xcm.model.TbDeviceLog;
import com.xcm.model.TbOperationLog;
import com.xcm.model.dto.DeviceLogListDTO;
import com.xcm.model.vo.DeviceLogListVO;
import com.xcm.page.PageInfo;
import com.xcm.service.DeviceLogService;
import com.xcm.service.OperationLogService;
import com.xcm.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 终端日志Service实现类
 */
@Service(value = "deviceLogService")
public class DeviceLogServiceImpl implements DeviceLogService {
	@Autowired
	private DeviceLogMapper deviceLogMapper;
	@Autowired
	private OperationLogService operationLogService;
	/**
	 * 保存终端日志
	 *
	 * @param deviceLog TbDeviceLog
	 */
	@Override
	@Transactional(readOnly = false, rollbackFor = Exception.class)
	public void saveDeviceLog(TbDeviceLog deviceLog) {
		deviceLogMapper.saveDeviceLog(deviceLog);
	}

	/**
	 * 获取日志
	 *
	 * @param mac mac地址
	 * @return List<TbDeviceLog>
	 */
	@Override
	public List<TbDeviceLog> getLogs(String mac) {
		return deviceLogMapper.listDeviceLog(mac);
	}

	/**
	 * 删除日志
	 *
	 * @param deviceLogId 终端id
	 */
	@Override
	@Transactional(readOnly = false, rollbackFor = Exception.class)
	public void deleteLogById(Long deviceLogId) {
		deviceLogMapper.deleteLogById(deviceLogId);
		//保存操作日志
		TbOperationLog operationLog = new TbOperationLog();
		operationLog.setOperationTypeId(OperationLogConstant.OPERATION_LOG_TYPE_DEVICE);
		operationLog.setOperationContent("删除id为" + deviceLogId + "的终端日志");
		operationLog.setIsSuccess(OperationLogConstant.OPERATION_LOG_SUCCESS);
		operationLog.setOperationTime(System.currentTimeMillis());
		// TODO: 2018/4/9 处理用户ID和IP地址
		//展示设置操作用户名、用户真实姓名、IP为空，用户保存在统一登录系统
		operationLog.setUserName("");
		operationLog.setRealName("");
		operationLog.setOperationIp("");
		operationLogService.saveOperationLog(operationLog);
	}

	/**
	 * 获取(终端)日志分页列表
	 *
	 * @param mac
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	@Override
	public PageInfo<DeviceLogListVO> listDeviceLogsByPage(String mac, Integer pageNum, Integer pageSize) {
		PageHelper.startPage(pageNum, pageSize);
		Page<DeviceLogListDTO> page = deviceLogMapper.listDeviceLogsByPage(mac);
		if (page == null) {
			return new PageInfo<>(pageNum, pageSize, 0, Lists.newArrayList());
		}
		//把Page转化为PageInfo
		PageInfo<DeviceLogListVO> pageInfo = new PageInfo<>();
		pageInfo.setPageNum(pageNum);
		pageInfo.setPageSize(pageSize);
		pageInfo.setTotal(page.getTotal());
		//把DTO转化为VO,并把时间戳转化为字符串
		List<DeviceLogListDTO> deviceLogDtoList = page.getResult();
		List<DeviceLogListVO> deviceLogVoList = Lists.newArrayList();
		for (DeviceLogListDTO dto : deviceLogDtoList) {
			DeviceLogListVO deviceLogVo = new DeviceLogListVO();
			deviceLogVo.setDeviceLogId(dto.getDeviceLogId());
			deviceLogVo.setName(dto.getName());
			Long uploadTime = dto.getUploadTime();
			if(uploadTime == null) {
				deviceLogVo.setUploadTime("");
			}else {
				deviceLogVo.setUploadTime(DateUtils.longTimeStampToStringDate(uploadTime, DateFormatConstant.LONG_DATE_FORMAT_STR));
			}
			deviceLogVoList.add(deviceLogVo);
		}
		pageInfo.setList(deviceLogVoList);
		return pageInfo;
	}

}
